package com.sl.hx.shop.ui.home.fragment;

import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sl.hx.shop.R;
import com.sl.hx.shop.base.BaseFragment;
import com.sl.hx.shop.base.BasePresenter;
import com.sl.hx.shop.bean.SearchResult;
import com.sl.hx.shop.http.ApiFactory;
import com.sl.hx.shop.http.HttpUtil;
import com.sl.hx.shop.http.ProgressSubscribe;
import com.sl.hx.shop.ui.home.activity.GoodsDetailActivity;
import com.sl.hx.shop.ui.home.adapter.HotListAdapter;
import com.trello.rxlifecycle2.android.FragmentEvent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

/**
 * 热销排行fragment
 */

public class HotListFragment extends BaseFragment {


    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh)
    SwipeRefreshLayout mRefresh;

    private HotListAdapter hotListAdapter;
    private List<SearchResult> searchResults = new ArrayList<>();
    private String hotlistKey;
    private int hotlistID;
    private int page = 1;
    private int num = 10;
    private boolean mIsRefresh = true;//刷新


    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int getViewLayoutId() {
        return R.layout.fragment_hotlist;
    }

    @Override
    protected void initView(View mView) {
        hotlistKey = getArguments().getString("hotlistKey");
        hotlistID = getArguments().getInt("hotlistId");

        initrecycle();
        getHotlist(page, num);
        //刷新
        refresh();
    }

    private void initrecycle() {
        hotListAdapter = new HotListAdapter(searchResults);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(hotListAdapter);
        hotListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                mIsRefresh=false;
                getHotlist(page, num);
            }
        },mRecyclerView);
        hotListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //跳转详情页
                Intent intent = new Intent(getActivity(), GoodsDetailActivity.class);
                intent.putExtra("num_iid", hotListAdapter.getItem(position).getNum_iid());
                intent.putExtra("user_type", hotListAdapter.getItem(position).getUser_type());
                startActivity(intent);
            }
        });
    }

    private void refresh() {
        mRefresh.setColorSchemeColors(ContextCompat.getColor(mContext, R.color.main_color));
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mIsRefresh = true;
                getHotlist(page, num);
            }
        });
    }

    private void getHotlist(int page, int num) {
        Observable observable = ApiFactory.getInstance().getHomeSearch(hotlistKey, 1,page, num);
        HttpUtil.getInstance().toSubscribe(observable, new ProgressSubscribe<List<SearchResult>>(mContext) {
            @Override
            protected void _onNext(List<SearchResult> searchResults) {
                showHotlist(searchResults);
            }

            @Override
            protected void _onError(String message) {
            }

        }, false, this, FragmentEvent.DESTROY);
    }

    private void showHotlist(List<SearchResult> searchResults) {
        if (mIsRefresh) {
            hotListAdapter.setNewData(searchResults);
        }else {
            if (searchResults != null && searchResults.size() > 0) {
                hotListAdapter.addData(searchResults);
            }
        }

        if (searchResults.size() < num) {
            hotListAdapter.loadMoreEnd(true);
        } else {
            hotListAdapter.loadMoreComplete();
        }

        //关闭刷新
        if (mRefresh.isRefreshing()){
            mRefresh.setRefreshing(false);
        }
    }

}
